home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / jcool01.zip / EX7_4.C < prev    next >
C/C++ Source or Header  |  1992-09-28  |  2KB  |  53 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12.  
  13. #include <cool/List.h>                // Include list header file
  14. #include <cool/Gen_String.h>            // Include COOL String class
  15. #include <cool/Iterator.h>            // Include COOL Iterator class
  16. #include "paragraph.h"                // Include Stroustrup text
  17.  
  18. #include <cool/List.C>
  19.  
  20. int main (void) {
  21.   CoolList<CoolGen_String> l1;                // Declare CoolList variable
  22.   CoolGen_String s;                    // Temporary string variable
  23.   int max_count = 0;                // Temporary counting variable
  24.   cout << text;                    // Output paragraph
  25.   text.compile ("[a-zA-Z]+");            // Match any alphabetical word
  26.   while (text.find ()) {            // While still more words
  27.     text.sub_string (s, text.start (), text.end ()); // Get word from paragraph
  28.     l1.push (s);                     // And add to CoolList
  29.   }
  30.   l1.reset ();                    // Invalidate current position
  31.   while (l1.next ()) {                // While there are still nodes
  32.     int counter = 0;                // Initialize counter
  33.     CoolGen_String cur_word;            // Temporary string variable
  34.     CoolIterator<CoolList<CoolGen_String> > i1 = l1.current_position ();    // Save current position
  35.     cur_word = l1.value ();            // Get word to be counted
  36.     l1.reset ();                // Invalidate current position
  37.  
  38.     while (l1.next ())                // While there are still nodes
  39.       if (l1.value () == cur_word)        // If word appears in CoolList
  40.     counter++;                // Increment usage count
  41.     if (counter > max_count) {            // If most used word so far
  42.       max_count = counter;            // Update maximum count
  43.       s = cur_word;                // And save word
  44.     }
  45.     l1.current_position () = i1;        // Restore old current position
  46.   }
  47.   cout << "There are " << l1.length () << " words\n";
  48.   l1.remove_duplicates ();            // Remove duplicate words
  49.   cout << "There are " << l1.length () << " unique words\n";
  50.   cout << "The most common word is `" << s << "' and is used " << max_count << " times\n";
  51.   return (0);                    // Exit with successful status
  52. }
  53.